#include <vcl.h>
#pragma hdrstop

#include "ctrlstd.hrc"
#include "CEditEuro.h"

#include <math.h>

#pragma package(smart_init)
#define NB_PIXEL_CONCEP 4
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé est utilisé pour être sûr que les composants créés n'ont
// aucune fonction virtuelle pure.
//
static inline void ValidCtrCheck(CEditEuro *)
{
    new CEditEuro(NULL);
}

//---------------------------------------------------------------------------
TColor __fastcall CEditEuro::ChangeCouleur ( void )
{
//Test la couleur a afficher en fonction des proprietes
//Color, ReadOnly et Enabled
    if (ComponentState.Contains( csDesigning )) {
        if ((m_bEnabled) && (!m_bReadOnly)){
            return m_FColor;
        }
        else if ((m_bEnabled) && (m_bReadOnly)){
                return PROG_COL_RDONLY;
            }
        else if ((!m_bEnabled) && (m_bReadOnly)){
                return PROG_COL_RDONLY_DISABLED;
            }
        else if ((!m_bEnabled) && (!m_bReadOnly)){
                return clBtnFace;
            }
    } else {
        if ((m_bEnabled) && (!m_bReadOnly)){
            return m_FColor;
        } else {
            return clBtnFace;
        }
    }
    return m_FColor;
}

//---------------------------------------------------------------------------
//Cherche si le Mask contient un separateur décimal
int __fastcall ChercheDecimalSep( AnsiString asText)
{
    int nPos = 0;

    if (asText != "") {
        int nPosDec;
        nPosDec  = asText.Pos( DecimalSeparator );
        if (nPosDec == 0) {
            nPosDec = asText.Pos( ',' );
            if (nPosDec == 0)
                nPosDec = asText.Pos( '.' );
        }
        if (nPosDec != 0)
            nPos = nPosDec;
    }

    return nPos;
}

//---------------------------------------------------------------------------
//Retire les Séparateurs de milliers
//Retire le symbole Euro
AnsiString __fastcall CEditEuro::SuppSeparMillier( AnsiString asText )
{
    int i = 0;
	int iSize = asText.Length();
    AnsiString asTemp;
    char Car;

    while( i < iSize ) {
        if(((Car = asText.c_str()[i]) != ' ' )
            && ((Car = asText.c_str()[i]) != '€' )
            && ((Car = asText.c_str()[i]) != 'F' )
            && ((Car = asText.c_str()[i]) != 'r' ))
            asTemp += AnsiString( Car );
        i ++;
    }

    return( asTemp );
}

//---------------------------------------------------------------------------
//Ajoute les Séparateurs de milliers
AnsiString __fastcall CEditEuro::AddSeparMillier( AnsiString asText)
{
    int         i;
    int         iCnt    = 0;
    AnsiString  asTemp  = asText;
    AnsiString  asFinal;
    AnsiString  asDec;

    if( asTemp.IsEmpty())
        return( AnsiString(""));

    asTemp = SuppSeparMillier( asTemp );

    i = asTemp.Length() - 1;

    // Ajoute les separateurs de milliers
    // Sauf pour les decimale
    /////////////////////////////////////
    bool bDec = true;
    while( i >= 0 ) {
        if( iCnt %3 == 0 && iCnt != 0 && !bDec) {
            if( !( i == 0 && asTemp.c_str()[ 0 ] == '-' ))
                asFinal.Insert( " ", 0 );
        }
        if (asTemp.c_str()[ i ] == DecimalSeparator)
            bDec = false;
        asFinal.Insert( asTemp.c_str()[ i ], 0 );
        iCnt ++;
        i --;
    }

    return( asFinal );
}

//---------------------------------------------------------------------------
double __fastcall CEditEuro::ConvFrToEu (double dVal, bool bFrancs)
{
    double dResult = 0;

    if (bFrancs) {
        dResult = dVal / 6.55957;
    } else {
        dResult = dVal * 6.55957;
    }

    //Limite ou supprime les décimal en fonction du mask
    int nPosDec         = ChercheDecimalSep(Mask);
    int nNbDec          = Mask.Length() - nPosDec;
    AnsiString asText   = FloatToStr(dResult);
    int nPosVal         = ChercheDecimalSep(asText);

    if (nPosDec) {
        if (nPosVal != 0) {
            dResult = StrToFloat(asText.SubString(1, nPosVal + nNbDec));
        } else {
            asText += DecimalSeparator;
            for (int i=0; i > nNbDec; i++)
                asText += "0";
            dResult = StrToFloat(asText);
        }
    } else {
        if (nPosVal != 0)   dResult = (floor(dResult + .5));
    }

    return dResult;
}

//---------------------------------------------------------------------------
__fastcall CEditEuro::CEditEuro(TComponent* Owner)
    : TCustomPanel(Owner)
{
    //Initialisation des propriétés
    m_bReadOnly             = false;    
    m_bVisible              = true;
    m_bEnabled              = true;
    m_bTabStop              = true;
//    m_bVisuEuro             = false;
    m_bVisuBouton           = true;
    m_nSizeEsp              = 10;
    m_asMask                = "#############";
    m_ValNS                 = I_NS;
    m_FMax                  = MAXMAX;
    m_FMin                  = m_ValNS;
	m_FColorNeg             = clRed;
    m_FColor                = clWindow;
    inherited::Color        = m_FColor;
    inherited::TabStop      = true;
    m_FTypeVal              = eInt;
    
    bFirstAff               = true;
    nEpaisseur              = 1;

    //Initialisation des composants
    BorderStyle             = bsSingle;
    BevelInner              = bvNone;
    BevelOuter              = bvNone;
    
    //Label pour afficher le libelle
    lLibelle                = new TLabel (this);
    lLibelle->Transparent   = true;
    lLibelle->FocusControl  = this;
    lLibelle->Caption = m_aLibelle;
    
    //Edit de saisie en euro
    EditPrinc               = new CEditPrinc( this);
    EditPrinc->Parent       = this;
    if (ComponentState.Contains( csDesigning ))
        EditPrinc->Left         = NB_PIXEL_CONCEP;
    else
        EditPrinc->Left         = 0;
    EditPrinc->Parent       = this;
    EditPrinc->Mask         = m_asMask;
    EditPrinc->BorderStyle  = bsNone;
    EditPrinc->OnEnter      = EditPrincEnter;
    EditPrinc->OnExit       = EditPrincExit;
    EditPrinc->OnChange     = OnChangeEdit;
    EditPrinc->OnKeyDown    = OnKeyDownEdit;
    EditPrinc->OnKeyUp      = OnKeyUpEdit;
    EditPrinc->TabStop      = false;
    EditPrinc->TypeValeur   = eInt;
//    EditPrinc->Text         = m_asText;

    Width   = EditPrinc->Width;
    Height  = EditPrinc->Height;

    if (ComponentState.Contains( csDesigning )) {
        newBouttonAff();
    }/* else {
        Width -= 18;
    }*/
}
//---------------------------------------------------------------------------
namespace Cediteuro
{
    void __fastcall PACKAGE Register()
    {
         TComponentClass classes[1] = {__classid(CEditEuro)};
         RegisterComponents("EIC Ctrlstd", classes, 0);
    }
}
//---------------------------------------------------------------------------
//Fonction permettant de recuperer les changements de taille
//et les appliques à tous les éléments
void __fastcall CEditEuro::WMWindowPosChanged( TWMWindowPosChanged &Msg)
{
    TCustomPanel::Dispatch( &Msg);
    Height = EditPrinc->Height;

    if (m_bVisuBouton && BouttonAff) {
        if (ComponentState.Contains( csDesigning )) {
            EditPrinc->Left         = NB_PIXEL_CONCEP;
            EditPrinc->Width        = Width - 5 - BouttonAff->Width - NB_PIXEL_CONCEP;
        } else {
            EditPrinc->Left         = 0;
            if (BorderStyle == bsSingle) {
                EditPrinc->Width        = Width - 5 - BouttonAff->Width;
            } else {
                EditPrinc->Width        = Width - BouttonAff->Width;
            }
        }
        if (BorderStyle == bsSingle) {
            BouttonAff->Left        = EditPrinc->Left + EditPrinc->Width + 1;
            BouttonAff->Height      = Height - 4;
        } else {
            BouttonAff->Left        = EditPrinc->Left + EditPrinc->Width;
            BouttonAff->Height      = Height;
        }

    } else {
        if (ComponentState.Contains( csDesigning )) {
            EditPrinc->Left         = NB_PIXEL_CONCEP;
            EditPrinc->Width        = Width - 4 - 2*NB_PIXEL_CONCEP;
        } else {
            if (BorderStyle == bsSingle) {
                EditPrinc->Left         = 0;
                EditPrinc->Width        = Width - 4;
            } else {
                EditPrinc->Left         = 0;
                EditPrinc->Width        = Width;
            }
        }
    }

    lLibelle->Top    = Top + 3;
    lLibelle->Left   = Left - lLibelle->Width - m_nSizeEsp;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::WMSetFocus( TWMSetFocus &Msg)
{
    EditPrinc->SetFocus();
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetReadOnly( bool bReadOnly )
{
    m_bReadOnly             = bReadOnly;
    EditPrinc->ReadOnly     = m_bReadOnly;
    TabStop                 = EditPrinc->TabStop;

    if (ComponentState.Contains( csDesigning )) {
        if (bReadOnly) {
            inherited::Color = ChangeCouleur();
        } else {
            EditPrinc->Brush->Color = ChangeCouleur();
            EditPrinc->Repaint();
            inherited::Color = ChangeCouleur();
        }
    } else {
            EditPrinc->Brush->Color = ChangeCouleur();
            EditPrinc->Repaint();
    }
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetVisuBouton( bool bVisu)
{
    m_bVisuBouton = bVisu;

    if(!m_bVisuBouton) {
        if (ComponentState.Contains( csDesigning )) {
            EditPrinc->Left         = NB_PIXEL_CONCEP;
            EditPrinc->Width = Width - 4 - 2*NB_PIXEL_CONCEP;
        } else {
            EditPrinc->Left         = 0;
            if (BorderStyle == bsSingle)
                EditPrinc->Width = Width - 4;
            else
                EditPrinc->Width = Width - 2;
        }
        if (ComponentState.Contains( csDesigning )) {
            BouttonAff->Left = Left + EditPrinc->Width;
        }
    } else {
        if (ComponentState.Contains( csDesigning )) {
            EditPrinc->Left         = NB_PIXEL_CONCEP;
            EditPrinc->Width = Width - 5 - BouttonAff->Width - NB_PIXEL_CONCEP;
        } else {
            EditPrinc->Left         = 0;
            if (BorderStyle == bsSingle)
                EditPrinc->Width = Width - 3 - BouttonAff->Width;
            else
                EditPrinc->Width = Width - 1 - BouttonAff->Width;
        }

        if (BorderStyle == bsSingle) {
            BouttonAff->Left = EditPrinc->Left + EditPrinc->Width + 1;
            BouttonAff->Height = Height - 4;
        } else {
            BouttonAff->Left = EditPrinc->Left + EditPrinc->Width;
            BouttonAff->Height = Height;
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetVisible( bool bValue)
{
    m_bVisible              = bValue;
    TCustomPanel::Visible   = m_bVisible;
    EditPrinc->Visible      = m_bVisible;
    lLibelle->Visible       = m_bVisible;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetEnabled( bool bValue)
{
    m_bEnabled              = bValue;
    TCustomPanel::Enabled   = m_bEnabled;
    EditPrinc->Enabled      = m_bEnabled;
    lLibelle->Enabled       = m_bEnabled;
    inherited::TabStop      = m_bTabStop && m_bEnabled;

    if (ComponentState.Contains( csDesigning )) {
        EditPrinc->Brush->Color = ChangeCouleur();
        EditPrinc->Repaint();
        inherited::Color = ChangeCouleur();
    } else {
        EditPrinc->Brush->Color = ChangeCouleur();
        EditPrinc->Repaint();
    }
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetFont( TFont *pFont)
{
    EditPrinc->Font->Assign( pFont);
    inherited::Font->Assign( pFont);
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetTabStop( bool bValue)
{
    m_bTabStop          = bValue;
    inherited::TabStop  = m_bTabStop && Enabled;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetLibelleFont( TFont *pFont)
{
    lLibelle->Font->Assign( pFont);
    lLibelle->Left   = Left - lLibelle->Width - m_nSizeEsp;

}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetLibelle( AnsiString aLibelle)
{
    m_aLibelle        = aLibelle;
    lLibelle->Parent  = Parent;
    lLibelle->Caption = m_aLibelle;
    lLibelle->Top     = Top + 3;
    lLibelle->Left    = Left  - lLibelle->Width - m_nSizeEsp;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetSizeEspace( int nValue)
{
    m_nSizeEsp     = nValue;
    lLibelle->Left = Left  - lLibelle->Width - m_nSizeEsp;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetColorNeg(TColor cCouleur )
{
    m_FColorNeg = cCouleur;
    EditPrinc->ColorNeg = m_FColorNeg;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetColor(TColor cCouleur )
{
    m_FColor = cCouleur;
    EditPrinc->Color = ChangeCouleur();
    inherited::Color = ChangeCouleur();
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetMask( AnsiString aMask)
{
    m_asMask        = aMask;
    EditPrinc->Mask = aMask;
    m_FMax          = EditPrinc->Max;
    MaxLength       = EditPrinc->MaxLength;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetMax(double dMax)
{
    EditPrinc->Max = dMax;
    m_FMax         = EditPrinc->Max;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetMaxLength( signed int iMaxLength )
{
    EditPrinc->MaxLength = iMaxLength;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetMin(double dMin)
{
    EditPrinc->Min  = dMin;
    MaxLength       = EditPrinc->MaxLength;
    m_FMin          = EditPrinc->Min;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetNSActive(bool bNSActive)
{
    EditPrinc->NSActive = bNSActive;

    if( EditPrinc->NSActive )
        m_FMin = EditPrinc->Min;
}

//---------------------------------------------------------------------------
/*
void __fastcall CEditEuro::SetText( AnsiString asTextOrigin )
{
    AnsiString asText = SuppSeparMillier(asTextOrigin);
    EditPrinc->Text = asText;
    m_asText = EditPrinc->Text;
}
*/
//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetValue( double dValeur )
{
    EditPrinc->Valeur = dValeur;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetTypeVal( enum TypeVal eTypeValeur )
{
    m_FTypeVal              = eTypeValeur;
    EditPrinc->TypeValeur   = eTypeValeur;
    m_FMax                  = EditPrinc->Max;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetTypeMonnaie( enum TypeMonnaieEdit eTypeMonnaie )
{
    EditPrinc->TypeMonnaie   = eTypeMonnaie;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::SetParentColor( bool bParentColor)
{
    m_bParentColor          = bParentColor;
    EditPrinc->ParentColor  = m_bParentColor;
    inherited::ParentColor  = m_bParentColor;
}

//---------------------------------------------------------------------------
enum TypeMonnaieEdit __fastcall CEditEuro::GetTypeMonnaie(void)
{
    return EditPrinc->TypeMonnaie;
}
//---------------------------------------------------------------------------
int __fastcall CEditEuro::GetMaxLength()
{
    return( EditPrinc->MaxLength );
}

//---------------------------------------------------------------------------
AnsiString __fastcall CEditEuro::GetMask()
{
    return( EditPrinc->Mask );
}

//---------------------------------------------------------------------------
AnsiString __fastcall CEditEuro::GetLibelle()
{
    return( m_aLibelle );
}


//---------------------------------------------------------------------------
bool __fastcall CEditEuro::GetTabStop()
{
    return inherited::TabStop;
}

//---------------------------------------------------------------------------
bool __fastcall CEditEuro::GetNSActive()
{
    return EditPrinc->NSActive;
}

//---------------------------------------------------------------------------
bool __fastcall CEditEuro::GetStateNS()
{
    return EditPrinc->StateNS;
}

//---------------------------------------------------------------------------
double __fastcall CEditEuro::GetValue()
{
	return( EditPrinc->Valeur );
}

//---------------------------------------------------------------------------
TFont *__fastcall CEditEuro::GetFont()
{
    return inherited::Font;
}

//---------------------------------------------------------------------------
TFont *__fastcall CEditEuro::GetLibelleFont()
{
    return lLibelle->Font;
}

//---------------------------------------------------------------------------
double __fastcall CEditEuro::GetValNS()
{
    return EditPrinc->ValeurNS;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::BouttonAffClick( TObject *pSender)
{
    if (!FormPopUp) newPopUp();

    //Ajoute le symbole Monétaire
    AnsiString asSymbole = "";
    bool bTypeMonnaie        = false;
    if (TypeMonnaie == edEuro) {
        asSymbole = "F";
    } else {
        asSymbole = "€";
        bTypeMonnaie        = true;
    }

    //Redimensionne la fenetre PopUp
    if ((EditPrinc->Valeur == I_NS) || (EditPrinc->Valeur == 0)) {
        lAffFranc->Visible = false;
        FormPopUp->Height = Height;
    } else {
        lAffFranc->Visible = true;
        FormPopUp->Height = Height + lAffFranc->Height;
        AnsiString asText = FloatToStr(EditPrinc->Valeur);//SuppSeparMillier (EditPrinc->Text);
        asText = FloatToStr(ConvFrToEu ( StrToFloat(asText), bTypeMonnaie));
        lAffFranc->Caption = AddSeparMillier(asText)+ " " + asSymbole + " ";
    }

    EditFranc->NSActive = true;

    PanelAff->Height        = FormPopUp->Height - (nEpaisseur * 2);
    EditFranc->Height       = PanelBas->Height;
    lAffSymboleFr->Height   = PanelBas->Height;

    FormPopUp->Width = Width;
    PanelAff->Width  = Width - (nEpaisseur * 2);
    lAffSymboleFr->Width = BouttonAff->Width;
    EditFranc->Width = PanelAff->Width - lAffSymboleFr->Width;
    lAffSymboleFr->Left = EditFranc->Width;

    // Place la fenêtre au bonne endroit
    TPoint point;
    point.x = Left - lAffSymboleFr->Width;
    point.y = Top + Height;
    point   = Parent->ClientToScreen( point);
    if (( point.x + FormPopUp->Width) > Screen->Width) point.x = ( Screen->Width-FormPopUp->Width);
    FormPopUp->Left = point.x;
    FormPopUp->Top  = point.y;

    // Donne la focalisation à la forme et la met au premier plan
    FormPopUp->Visible = true;
    if (!bFirstAff)
        ShowPopUp();
    FormPopUp->SetFocus();
    FormPopUp->BringToFront();

    //Surtout ne pas modifier ce If
    if (bFirstAff) {
        bFirstAff = false;
        BouttonAffClick( BouttonAff );
    }

    //Ajoute le symbole Monnetaire
    if (EditPrinc->TypeMonnaie == edEuro)
        asSymbole = "€";
    else
        asSymbole = "F";

/*
    if (m_bVisuEuro) {
        AnsiString asTextEuro = EditPrinc->Text;
        if (!asTextEuro.IsEmpty()) {
            if (asTextEuro.Pos(asSymbole) == 0)
                asTextEuro += " " + asSymbole + " ";
        }
        EditPrinc->Text = asTextEuro;
    }
*/    
    EditFranc->Clear();
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::FormPopUpOnDeactivate( TObject *pSender)
{
    HidePopUp();
//    if (EditPrinc->Text != "") {
        //AnsiString asText = SuppSeparMillier(EditPrinc->Text);
        if (EditPrinc->Valeur >= 0)
            EditPrinc->Font->Color  = inherited::Font->Color;
        else
            EditPrinc->Font->Color  = m_FColorNeg;
//        EditPrinc->Text = asText;
//    }

    EditPrinc->SetFocus();
    EditPrinc->SelectAll();
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::EditFrancKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
    if (Key == VK_ESCAPE) {
//        EditPrinc->Text = SuppSeparMillier(EditPrinc->Text);

        //Sélectionne tout le texte
/*
        if (EditPrinc->Text.Length() != 0) {
            EditPrinc->SelStart  = 0;
            EditPrinc->SelLength = EditPrinc->Text.Length();
        }
*/
        FormPopUp->Visible = false;
    }

    if ((Key == VK_RETURN) || (Key == VK_TAB) || ((Key == VK_TAB) && Shift.Contains(ssShift))) {
        if (EditFranc->Valeur != I_NS) {
            bool bTypeMonnaie        = false;

            if (TypeMonnaie == edEuro) bTypeMonnaie = true;

            //EditPrinc->Text = FloatToStr (ConvFrToEu ( StrToFloat(EditFranc->Text), bTypeMonnaie));
            EditPrinc->Valeur = ConvFrToEu ( EditFranc->Valeur, bTypeMonnaie);
        }/* else {
            EditPrinc->Text = SuppSeparMillier(EditPrinc->Text);

            //Sélectionne tout le texte
            if (EditPrinc->Text.Length() != 0) {
                EditPrinc->SelStart  = 0;
                EditPrinc->SelLength = EditPrinc->Text.Length();
            }
        }*/
        FormPopUp->Visible = false;
    }
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::EditPrincKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
    if (Key == VK_SPACE) {
        if (BouttonAff == NULL) newBouttonAff();
        BouttonAffClick(BouttonAff);
    }

    if (Key == VK_RETURN) {
        TList *lListTab = new TList();
        dynamic_cast<TWinControl*>(Parent)->GetTabOrderList(lListTab);

        int nIndex = lListTab->IndexOf(this);
        bool bTrouve = false;

        if (lListTab->Count > 0) {
            int i = lListTab->Count;
            do {
                if (nIndex >= lListTab->Count || (nIndex + 1) >= lListTab->Count)
                    break;

                TWinControl *CtrlCurr = (TWinControl*)lListTab->Items[nIndex + 1];

                if (CtrlCurr->CanFocus() && CtrlCurr->TabStop) {
                    CtrlCurr->SetFocus();
                    bTrouve=true;
                    break;
                } else {
                    nIndex += 1;
                }
                i -- ;
            } while (i >= 0);

        }
        if (!bTrouve) {
            int nTemp = lListTab->Count;

            lListTab->Clear();
            
            if (Parent->Parent)
                dynamic_cast<TWinControl*>(Parent->Parent)->GetTabOrderList(lListTab);

            int nIndex = lListTab->IndexOf(Parent) + nTemp - 1;

            if (lListTab->Count > 0) {
                int i = lListTab->Count;
                do {
                    if ((nIndex >= lListTab->Count) || ((nIndex + 1) >= lListTab->Count))
                        nIndex = 0;

                    TWinControl *CtrlCurr = (TWinControl*)lListTab->Items[nIndex + 1];

                    if (CtrlCurr->CanFocus() && CtrlCurr->TabStop) {
                        CtrlCurr->SetFocus();
                        break;
                    } else {
                        nIndex += 1;
                    }
                    i -- ;
                } while (i >= 0);
            }
        }

        delete lListTab;
    }
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::EditPrincEnter(TObject *Sender)
{
    detruitPopUp();

    //Ajoute le symbole Monétaire
    AnsiString asSymbole = "";
    if (EditPrinc->TypeMonnaie == edEuro)
        asSymbole = "€";
    else
        asSymbole = "F";

    //Retire le symbole Euro
    /*if (m_bVisuEuro) {
        AnsiString asText = EditPrinc->Text;
        int nPos;
        if (!asText.IsEmpty()) {
            nPos = asText.Pos(asSymbole);
            if (nPos != 0) {
                if (EditPrinc->TypeMonnaie == edEuro)
                    asText.Delete(nPos, 1);
                else
                    asText.Delete(nPos, 2);
            }
            EditPrinc->Text = asText;
        }
    }*/

    //Sélectionne tout le texte
    /*if (EditPrinc->Text.Length() != 0) {
        EditPrinc->SelStart  = 0;
        EditPrinc->SelLength = EditPrinc->Text.Length();
    }*/

    //Affiche  le bouton en rentrant dans le Edit
    if (m_bVisuBouton) {
        if (BouttonAff == NULL) newBouttonAff();
    }
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::EditPrincExit(TObject *Sender)
{

    //Ajoute le symbole Monétaire
    AnsiString asSymbole = "";
    if (EditPrinc->TypeMonnaie == edEuro)
        asSymbole = "€";
    else
        asSymbole = "F";

    /*if (m_bVisuEuro) {
        AnsiString asText = EditPrinc->Text;
        if (!asText.IsEmpty()) {
            if (asText.Pos(asSymbole) == 0)
                asText += " " + asSymbole + " ";
            EditPrinc->Text = asText;
        }
    }*/

    //Efface le bouton a la sortie du composant
    if (m_bVisuBouton) {
        detruitBouttonAff();
    }
}

//---------------------------------------------------------------------------

void __fastcall CEditEuro::ShowPopUp()
{
    if (FOnShowPopUp != NULL)   FOnShowPopUp(FormPopUp);
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::HidePopUp()
{
    if (FOnHidePopUp != NULL)   FOnHidePopUp(FormPopUp);
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::OnChangeEdit( TObject *Sender )
{
    if (FOnChange != NULL)   FOnChange(Sender);
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::OnKeyDownEdit( TObject *Sender, Word &Key, TShiftState Shift )
{
    EditPrincKeyDown(Sender, Key, Shift);
    
    if (FOnKeyDown != NULL)   FOnKeyDown(Sender, Key, Shift);
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::OnKeyUpEdit( TObject *Sender, Word &Key, TShiftState Shift )
{
    if (TypeMonnaie == edEuro) {
        if ((Key == 'f') || (Key == 'F'))
            OnChangeEdit(Sender);
    } else {
        if ((Key == 'e') || (Key == 'E'))
            OnChangeEdit(Sender);
    }
    
    if (FOnKeyUp != NULL)   FOnKeyUp(Sender, Key, Shift);
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::Clear(void)
{
    Valeur = ValeurNS;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::newPopUp(void)
{
    //Forme d'affichage et de saisie des francs
    FormPopUp               = new TForm (this);
    FormPopUp->BorderStyle  = bsNone;
    FormPopUp->OnDeactivate = FormPopUpOnDeactivate;

    //Objet pour dessiner un rectangle autour de FormPopUp
    sRectTour               = new TShape (FormPopUp);
    sRectTour->Parent       = FormPopUp;
    sRectTour->Brush->Style = bsClear;
    sRectTour->Pen->Width   = nEpaisseur;
    sRectTour->Align        = alClient;

    //Panel pour afficher en style combobox
    PanelAff                = new TPanel (FormPopUp);
    PanelAff->Parent        = FormPopUp;
    PanelAff->Left          = nEpaisseur;
    PanelAff->Top           = nEpaisseur;
    PanelAff->Color         = clWindow;
    PanelAff->BevelOuter    = bvNone;
    PanelAff->BevelInner    = bvNone;
    PanelAff->BorderStyle   = bsNone;

    //Affichage du montant en francs
    lAffFranc               = new TLabel (PanelAff);
    lAffFranc->Parent       = PanelAff;
    lAffFranc->Left         = 0;
    lAffFranc->Top          = 0;
    lAffFranc->Alignment    = taRightJustify;
    lAffFranc->Align        = alTop;
    lAffFranc->Height       = 13;
    lAffFranc->Color        = clBtnFace;

    //Panel pour afficher le bas de la form
    PanelBas                = new TPanel (PanelAff);
    PanelBas->Parent        = PanelAff;
    PanelBas->Align         = alClient;
    PanelBas->BevelOuter    = bvNone;
    PanelBas->BevelInner    = bvNone;
    PanelBas->BorderStyle   = bsNone;

    //Saisie du montant en francs
    EditFranc               = new CEditNum (PanelBas);
    EditFranc->Parent       = PanelBas;
    EditFranc->Left         = 0;
    EditFranc->Top          = 0;
    EditFranc->OnKeyDown    = EditFrancKeyDown;
    EditFranc->Font->Assign( EditPrinc->Font);
    EditFranc->ColorNeg     = EditPrinc->ColorNeg;
    EditFranc->Color        = clInfoBk;
    EditFranc->Mask         = m_asMask;
    EditFranc->Max          = m_FMax;
    EditFranc->MaxLength    = EditPrinc->MaxLength;
    EditFranc->Min          = m_FMin;
    EditFranc->TypeValeur   = m_FTypeVal;

    //Affichage du symbole des francs a cote de la saisie
    lAffSymboleFr               = new TLabel (PanelBas);
    lAffSymboleFr->Parent       = PanelBas;
    lAffSymboleFr->Width        = 17;
    lAffSymboleFr->Top          = 0;
    lAffSymboleFr->Caption      = "F";
    lAffSymboleFr->Alignment    = taCenter;
    lAffSymboleFr->Layout       = tlCenter;
    if (EditPrinc->TypeMonnaie == edEuro)
        lAffSymboleFr->Caption      = "F";
    else
        lAffSymboleFr->Caption      = "€";

}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::detruitPopUp(void)
{
    bFirstAff = true;

    if (sRectTour) {
        delete sRectTour;
        sRectTour = NULL;
    }
    if (lAffSymboleFr) {
        delete lAffSymboleFr;
        lAffSymboleFr = NULL;
    }
    if (EditFranc) {
        delete EditFranc;
        EditFranc = NULL;
    }
    if (PanelBas) {
        delete PanelBas;
        PanelBas = NULL;
    }
    if (lAffFranc) {
        delete lAffFranc;
        lAffFranc = NULL;
    }
    if (PanelAff) {
        delete PanelAff;
        PanelAff = NULL;
    }
    if (FormPopUp) {
        delete FormPopUp;
        FormPopUp = NULL;
    }
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::newBouttonAff(void)
{
    //Boutton pour lancer l'affichage de la saisie en Francs
    BouttonAff              = new TSpeedButton( this);
    BouttonAff->Left        = EditPrinc->Left + EditPrinc->Width + 1;
    BouttonAff->Parent      = this;
    BouttonAff->OnClick     = BouttonAffClick;
    BouttonAff->Width       = 17;
    BouttonAff->NumGlyphs   = 2;
    BouttonAff->Glyph->LoadFromResourceName(( int)HInstance, "ARROW");
    BouttonAff->Visible     = true;
    
    if (BorderStyle == bsSingle)
        Width   += BouttonAff->Width + 1;
    else
        Width   += BouttonAff->Width;
}

//---------------------------------------------------------------------------
void __fastcall CEditEuro::detruitBouttonAff(void)
{
    if (BouttonAff) {
        int nTemp = BouttonAff->Width;

        delete BouttonAff;
        BouttonAff = NULL;
        if (BorderStyle == bsSingle)
            Width -= (nTemp + 1);
        else
            Width -= nTemp;
    }
}

//---------------------------------------------------------------------------



//---------------------------------------------------------------------------

//----------------------------Classe CEditPrinc------------------------------

//---------------------------------------------------------------------------

__fastcall CEditPrinc::CEditPrinc(TComponent* Owner)
    : CEditNum(Owner)
{
    //Initialisation des propriétés
}
//---------------------------------------------------------------------------
double __fastcall CEditPrinc::Conv(double dVal, bool bFrancs)
{
    double dResult = 0;

    if (bFrancs) {
        dResult = dVal / 6.55957;
    } else {
        dResult = dVal * 6.55957;
    }

    //Limite ou supprime les décimal en fonction du mask
    int nPosDec         = ChercheDecimalSep(Mask);
    int nNbDec          = Mask.Length() - nPosDec;
    AnsiString asText   = FloatToStr(dResult);
    int nPosVal         = ChercheDecimalSep(asText);

    if (nPosDec) {
        if (nPosVal != 0) {
            dResult = StrToFloat(asText.SubString(1, nPosVal + nNbDec));
        } else {
            asText += DecimalSeparator;
            for (int i=0; i > nNbDec; i++)
                asText += "0";
            dResult = StrToFloat(asText);
        }
    } else {
        if (nPosVal != 0)   dResult = (floor(dResult + .5));
    }

    return dResult;
}

//---------------------------------------------------------------------------
void __fastcall CEditPrinc::WMChar( Messages::TWMChar& Msg)
{
   switch (Msg.CharCode) {
    case VK_BACK :
        inherited::Dispatch( &Msg);
        break;
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':   ClearSelection();
                if((SelStart == 0) && (Font->Color == ColorNeg))
                    Font->Color = clWindowText;
                if ((Valeur >= ValeurNS) && (Valeur <= (-ValeurNS)))
                    inherited::Dispatch( &Msg);
               break;
    case 'F':
    case 'f':  {
                   if ((Valeur != I_NS) && (m_TypeMonnaie == edEuro)){
                       double dEuro = Conv( Valeur, true);
                       Valeur = dEuro;
                       SelectAll();
                   } else {
                    MessageBeep( 0xFFFFFFFF);
                    Msg.Result = 0;
                   }
               }
               break;
    case 'E':
    case 'e':  {
                   if ((Valeur != I_NS) && (m_TypeMonnaie == edFranc)){
                       double dEuro = Conv( Valeur, false);
                       Valeur = dEuro;
                       SelectAll();
                   } else {
                    MessageBeep( 0xFFFFFFFF);
                    Msg.Result = 0;
                   }
               }
               break;
    case ',':
    case '.': {
                    if ((SelStart != 0) && (Valeur < Max)
                        && (ChercheDecimalSep(Mask) != 0)) {
                            Msg.CharCode = DecimalSeparator;
                            inherited::Dispatch( &Msg);
                    }
                  }
                  break;
    case '-':  if( ((SelStart == 0) && (Min < 0)) || (Valeur != 0) ) {
    			   Font->Color = ColorNeg;
                   inherited::Dispatch(&Msg);
               }
				break;
    default:   MessageBeep( 0xFFFFFFFF);
               Msg.Result = 0;
   }

}

//---------------------------------------------------------------------------
